package friendService

import (
	"chart.zozoo.net/app/service/userService"
	"errors"
	"fmt"
)

type SearchFriendInfo struct {
	Uuid          string `json:"uuid"`
	City          string `json:"city"`
	Profile       string `json:"profile"`
	Username      string `json:"username"`
	Headimgurl    string `json:"headimgurl"`
	Nickname      string `json:"nickname"`
	FriendsStatus bool   `json:"friends_status"`
}

// Search 搜索好友
func Search(uuid string, keywords string) (friendInfo *SearchFriendInfo, err error) {
	//获取用户信息
	u, err := userService.InfoByUuid(uuid)
	if err != nil {
		return nil, err
	}

	//搜索用户信息
	findUser, err := userService.InfoByKeywords(keywords)
	if err != nil {
		return nil, err
	}
	if findUser.Id == 0 {
		return nil, errors.New("用户不存在")
	}
	if findUser.Id == u.Id {
		return nil, errors.New("不能添加自己到通讯录")
	}

	friendInfo = &SearchFriendInfo{
		Uuid:          findUser.Uuid,
		City:          findUser.City,
		Profile:       findUser.Profile,
		Username:      findUser.Username,
		Headimgurl:    findUser.Headimgurl,
		Nickname:      findUser.Nickname,
		FriendsStatus: false,
	}

	fmt.Println(friendInfo)
	//判断好友状态
	uf, err := Info(u.Id, findUser.Id)
	if err != nil {
		return nil, err
	}
	if uf != nil {
		friendInfo.FriendsStatus = true
	}

	return friendInfo, err
}
